package com.dst.service.impl;

import com.dst.dao.TeasMapper;
import com.dst.eneity.Category;
import com.dst.eneity.Tea;
import com.dst.service.TeaService;
import com.dst.utils.MybatisUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class TeaServiceImpl implements TeaService {
    @Override
    public List<Tea> selectAll() {
        List<Tea> teas=null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeasMapper mapper = sqlSession.getMapper(TeasMapper.class);
            teas = mapper.selectAll();
        } finally {
            sqlSession.close();
        }
        return teas;
    }

    @Override
    public void delete(int id) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeasMapper mapper = sqlSession.getMapper(TeasMapper.class);
            mapper.delete(id);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public void add(Tea tea) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeasMapper mapper = sqlSession.getMapper(TeasMapper.class);
            mapper.add(tea);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public void update(Tea tea) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeasMapper mapper = sqlSession.getMapper(TeasMapper.class);
            mapper.update(tea);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }


    @Override
    public Tea selectById(int id) {
        SqlSession sqlSession=null;
        Tea tea=null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeasMapper mapper = sqlSession.getMapper(TeasMapper.class);
            tea = mapper.selectById(id);

        } finally {
            sqlSession.close();
        }
        return tea;
    }

    @Override
    public void deleteCateAndTea(int id) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeasMapper mapper = sqlSession.getMapper(TeasMapper.class);
            mapper.deleteCateAndTea(id);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public void deleteBatch(int[] ids) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeasMapper mapper = sqlSession.getMapper(TeasMapper.class);
            mapper.deleteBatch(ids);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public PageInfo<Tea> selectAllTea(String name) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeasMapper mapper = sqlSession.getMapper(TeasMapper.class);
        List<Tea> teas = mapper.selectTeaAll(name);
        PageInfo<Tea> pageInfo=new PageInfo<>(teas);
        return pageInfo;
    }

    @Override
    public PageInfo<Tea> selectLimitAll(int curpage,int pageSize) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeasMapper mapper = sqlSession.getMapper(TeasMapper.class);
        PageHelper.startPage( curpage, pageSize);
        List<Tea> teas = mapper.selectAll();
        PageInfo<Tea> pageInfo=new PageInfo<>(teas);
        return pageInfo;
    }

    @Override
    public PageInfo<Tea> selectLikeAll(String name, Double price) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeasMapper mapper = sqlSession.getMapper(TeasMapper.class);
        List<Tea> teas = mapper.selectLikeAll(name, price);
        PageInfo<Tea> pageInfo=new PageInfo<>(teas);
        return pageInfo;
    }


}
